package cn.jobstart.pubInter;


import cn.jobstart.common.StringUtil;
import cn.jobstart.common.redis.RedisUtil;
import cn.jobstart.pubInter.baseVO.DecoratePageList;
import cn.jobstart.pubInter.common.PageBean;
import cn.jobstart.pubInter.common.PubConst;
import cn.jobstart.pubInter.exception.BusinessException;
import cn.jobstart.pubInter.exception.BusinessExceptionEnum;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.web.common.SkJsonResult;
import cn.jobstart.web.common.token.JwtsUtil;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sunke
 * @Date 2019-09-09 16:31:10
 * @description 前台controller基础
 */

public class FrontController<T, PageVO, Search extends BaseSearch> extends BaseController {

    private static String fileBaseUrl;
    private static String baseRoot;

    public String getBaseRoot(){
        return FrontController.baseRoot;
    }

    @Value("${file.upload.baseUrl}")
    public String baseUrl;
    @Resource(name = "jwtsUtil")
    private JwtsUtil jwtsUtil;
    @Resource
    private RedisUtil redisUtil;
    @Value("${debugRun}")
    private boolean debug;

    private static String baseTempRoot;

    @Value("${file.upload.tempRoot}")
    public void setBaseTempRoot(String tempRoot) {
        FrontController.baseTempRoot = tempRoot;

    }


    public String getBaseTempRoot() {
        return FrontController.baseTempRoot;
    }


    public static String getFileBaseUrl() {
        return fileBaseUrl;
    }

    @Value("${file.upload.website}")
    public void setFileBaseUrl(String fileBaseUrl) {
        FrontController.fileBaseUrl = fileBaseUrl;
    }




    /**
     * 得到完整的图片列表
     *
     * @param img
     * @return
     */
    public static String getIntactImg(String img) {

        if (StringUtil.isEmpty(img)) {
            return "";
        }
        return FrontController.getFileBaseUrl() + "/" + img;

    }








    /**
     * 得到完整的图片列表
     *
     * @param imgs
     * @return
     */
    public static List<String> getIntactImg(List<String> imgs) {

        if (StringUtil.isNullOrEmpty(imgs)) {
            return null;
        }

        return imgs.stream().map(
                item -> FrontController.getFileBaseUrl() + "/" + item).collect(Collectors.toList());


    }


    public static List<FileVO> getFileNamedAndImgPathList(String imgs){

        if (StringUtil.isNullOrEmpty(imgs)) {
            return null;
        }
        List<String> imgStrs = Arrays.stream(imgs.split("@@")).collect(Collectors.toList());
        if (StringUtil.isNullOrEmpty(imgStrs)) {
            return null;
        }

        return imgStrs.stream().map(item -> {
            try {
                String[] tmp = item.split("##");
                if(tmp!=null && tmp.length==1){
                    return new FileVO(tmp[0], FrontController.getIntactImg(tmp[0]),tmp[0]);
                }else{
                    return new FileVO(tmp[0], FrontController.getIntactImg(tmp[1]),tmp[1]);
                }


            } catch (Exception e) {
            }
            return null;
        }).filter(item -> StringUtil.isNotObjEmpty(item)).collect(Collectors.toList());


    }



    /**
     * 得到完整的图片列表
     *
     * @param img
     * @return
     */
    public static List<String> getImgListByImgStr(String img) {

        List<String> imgs = null;
        if (StringUtil.isNotEmpty(img)) {

            String[] imgList = img.split(",");

            imgs = new ArrayList<>();
            for (String item : imgList) {
                imgs.add(FrontController.getFileBaseUrl() + "/" + item);
            }

        }

        return imgs;

    }

    @Value("${file.upload.root}")
    public void setBaseRoot(String baseRoot) {
        FrontController.baseRoot = baseRoot;

    }





    public RedisUtil getRedisUtil() {
        return redisUtil;
    }

    public JwtsUtil getJwtsUtil() {
        return jwtsUtil;
    }



    public boolean isDebug() {
        return debug;
    }



    protected IBaseService getBaseService() throws SkException {
        throw new BusinessException(BusinessExceptionEnum.SERVICE_EMPTY);

    }

    public final Class<PageVO> getPageVOClass() {
        ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
        return (Class) type.getActualTypeArguments()[1];
    }

    public SkJsonResult<PageVO> getList(Search search) throws SkException {
        SkJsonResult<PageVO> result = SkJsonResult.ok();
        Map<String, Object> dataMap = getBaseService().paginate(search);

        DecoratePageList pageList = new DecoratePageList(decorateList((List<T>) dataMap.get(PubConst.PAGINATE_DATA_KEY)), (PageBean) dataMap.get(PubConst.PAGINATE_PAGE_KEY));
        result.setData(map(pageList, getPageVOClass()));
        return result;
    }

    /**
     * 装饰list数据
     *
     * @param dataList
     * @return
     */
    public List<T> decorateList(List<T> dataList) {
        return dataList;
    }


}